package br.com.knowledge.persistence.hibernate;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;

import br.com.knowledge.helper.LoadRegexFilters;
import br.com.knowledge.persistence.hibernate.dao.BcdTabelasDAO;
import br.com.knowledge.persistence.hibernate.entities.BcdTabelas;
import br.com.knowledge.persistence.hibernate.entities.CadLinguagem;
import br.com.knowledge.persistence.hibernate.entities.FntFonte;

public class Teste {
	public static void main(String[] args) {
		try {
			CadLinguagem lingua = new CadLinguagem();
			lingua.setNmlinguagem("sqlwindows");			
			BcdTabelasDAO tabDao = new BcdTabelasDAO();
			String objs[] = null;
			List<String> tabelas = new ArrayList<String>();
			List<String> tabelasLoadFilter = new ArrayList<String>();
			List<String> tabelasFromDb = new ArrayList<String>();
			//File codigoFonte = new File("C:\\Users\\Cinq\\Workspaces\\david\\project\\knowledge\\src\\prc_1014.apt");
			//File codigoFonte = new File("E:\\WTF\\desenvolvimento\\sam\\prc_025.apt");
			//File codigoFonte = new File("C:\\Users\\salomao\\Workspaces\\MyEclipse 8.x\\knowledge\\src\\prc_1014.apt");
			File codigoFonte = new File("C:\\temp\\prc_1014.apt");
			String codigo = FileUtils.readFileToString(codigoFonte, "UTF-8");						
			//String pattern = LoadRegexFilters.getProperties(lingua).getProperty("pattern.1", "sqlwindows");
			String pattern = "(Form Window|Dialog Box|MDI Window|Table Window):\\s([a-zA-Z0-9_]+).+((!?MDI)+|(!?From)+|(!?Table)+|(!?Dialog)+)";
			Pattern MY_PATTERN = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
			Matcher m = MY_PATTERN.matcher(codigo);
			while (m.find()) {
				String s = m.group(0);
				s = s.replaceAll("\t|\n|\r|\\s+", " ");
		    	s = s.replaceAll("from|From|FROM", "");
		    	s = s.replaceAll("into|Into|INTO", "");
		    	s = s.replaceAll("where|Where|WHERE", "");
		    	s = s.replaceAll("union|Union|UNION", "");
		    	s = s.replaceAll("select|Select|SELECT", "");
		    	s = s.replaceAll("and|And|AND", "");
		    	s = s.replaceAll("or|Or|OR", "");
		    	s = s.replaceAll("\\s:([^\\s])*", "");
		    	objs = s.split(" |, ");
		    	for(String ss:objs){
		    		if(!ss.isEmpty()){
		    			tabelas.add(ss);
		    		}
		    	}
			}
			
			//limpa duplicatas
			for(String ss:tabelas){
	    		if(!tabelasLoadFilter.contains(ss)){
	    			tabelasLoadFilter.add(ss);
	    		}
	    	}
			
			//valida nome com tabela cadastrada no banco
			for(String ss:tabelasLoadFilter){
	    		BcdTabelas tableLoaded = tabDao.findByName(ss);
	    		if(tableLoaded!=null){
	    			tabelasFromDb.add(tableLoaded.getNometabela());
	    		}else{
	    			System.out.println("Does not match with know tables: "+ss);
	    		}
	    	}
			System.out.println("\nQuantidade de Tabelas utilizadas ="+tabelasFromDb.size());
			
			System.out.println("Lista de tabelas encontradas:");
			for(String t:tabelasFromDb){
				System.out.println(t);
				FntFonte fonte = new FntFonte();
				fonte.setNomefonte(codigoFonte.getName());
				
				
			}
						
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
